package xyz.zq.cloud.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import xyz.zq.cloud.admin.mapper.CompanyMapper;
import xyz.zq.cloud.admin.service.CompanyService;
import xyz.zq.cloud.bs.api.entity.Company;
import xyz.zq.cloud.bs.api.type.AccountType;
import xyz.zq.cloud.common.core.constant.DBConstant;
import xyz.zq.cloud.common.core.util.R;

/**
 * <p>
 * 租户信息 服务实现类
 * </p>
 *
 * @author hzq
 * @since 2020-05-11
 */
@Slf4j
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    /**
     * 通过后缀获取租户信息
     */
    @Override
    public R<Company> getCompanyBySuffix(String suffix) {

        Company company = getOne(Wrappers.<Company>query()
                .lambda().eq(Company::getSuffix, suffix)
                .eq(Company::getDel, DBConstant.NO_DEL));

        if (ObjectUtil.isEmpty(company)) {
            log.warn("租户不存在");
            return R.failed("租户不存在");
        }

        if (company.getExpired() == AccountType.EXPIRED) {
            log.warn("租户[{}]已过期，请联系管理员", company.getId());
            return R.failed("租户已过期，请联系管理员");
        }

        if (company.getLocked() == AccountType.LOCKED) {
            log.warn("租户[{}]已锁定，请联系管理员", company.getId());
            return R.failed("租户已锁定，请联系管理员");
        }
        if (company.getEnabled() == AccountType.NOT_ENABLED) {
            log.warn("租户[{}]未启用，请联系管理员", company.getId());
            return R.failed("租户未启用，请联系管理员");
        }

        return R.ok(company);
    }
}
